package hn.cch.spring.aop;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.support.StaticMethodMatcherPointcut;
import org.springframework.core.annotation.MergedAnnotations;
import org.springframework.core.annotation.MergedAnnotations.SearchStrategy;
import org.springframework.lang.NonNull;
import org.springframework.web.bind.annotation.RequestMapping;

import java.lang.reflect.Method;

public class SpringAopDemo {

    private static final Logger logger = LoggerFactory.getLogger(SpringAopDemo.class);

    public static void main(String[] args) {

        // RequestMapping切点实现原理
        StaticMethodMatcherPointcut pointcut = new StaticMethodMatcherPointcut() {
            @Override
            public boolean matches(@NonNull Method method, @NonNull Class<?> targetClass) {
                MergedAnnotations annotations = MergedAnnotations.from(method);// 匹配方法上的注解
                if (annotations.isPresent(RequestMapping.class)) {
                    return true;
                }
                annotations = MergedAnnotations.from(targetClass, SearchStrategy.DIRECT);// 匹配类上的注解
                if (annotations.isPresent(RequestMapping.class)) {
                    return true;
                }
                annotations = MergedAnnotations.from(targetClass, SearchStrategy.TYPE_HIERARCHY);// 匹配实现接口上的注解
                if (annotations.isPresent(RequestMapping.class)) {
                    return true;
                }
                return false;
            }
        };
    }


}
